package com.tear.language.node;

import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import ognl.Ognl;

import org.apache.commons.lang.StringUtils;

import com.tear.language.utils.Attrs;

/**
判断操作
属性如下
condition 判断条件支持ognl表达式
*/
public class IfNode extends BaseNode {

	@Override
	public boolean parse(Map<String, Object> currParams,Map<String, Object> globalParams, JSONObject obj) throws Exception {
		
		//解析if时先清空过程变量
		currParams.remove(Attrs.PROCESS_VAR);
		
		String testStr = obj.optString(Attrs.CONDITION);
		
		boolean test = false;
		try {
			if(StringUtils.isNotEmpty(testStr)) {
				Map<String, Object> allParams = getAllParams(currParams,globalParams);
				test = (Boolean)Ognl.getValue(testStr,allParams);
			}
		} catch (Exception e) {
			throw new Exception("判断操作参数"+testStr+"不合法");
		}
		JSONArray array = obj.optJSONArray(Attrs.DO);
		if(array != null && array.size() == 0) {
			test = true;
		}
		//如果没有进入if节点的处理体do中，则传入过程变量到下一个else节点,其实就是条件取非
		if(!test) {
			currParams.put(Attrs.PROCESS_VAR, "!("+testStr+")");
		}
		return test;
	}

}
